//
//  CalculatorAppViewController.m
//  CalculatorApp
//
//  Created by Michael Geering on 26/02/2010.
//  Copyright SDC 2010. All rights reserved.
//

#import "CalculatorAppViewController.h"
#import "CalculatorAppAppDelegate.h"


@implementation CalculatorAppViewController

@synthesize button1, button2, button3, button4, button5, button6, button7, button8, button9, button0,
buttonMinus, buttonMultiply, buttonDivide, buttonPlus, calcDisplay, buttonEquals, buttonClear,
buttonRand, buttonPosNeg, xCoordArray, yCoordArray, buttonIdentityRemain, buttonIdentity, buttonDecimal,
xCoordRemain, yCoordRemain, snarkyMessages, modifier;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	// 2 arrays for storing the x and y coordinates of all movable buttons.
	// Stored in order from top-left to bottom-right.
	// Does not include large buttons such as equals.
	
	[self setModifier:[[NSArray alloc] initWithObjects:@"+",@"-",@"/",@"*",nil]];
	[self setXCoordArray:[[NSArray alloc] initWithObjects:@"8", @"86", @"164", @"242", @"8", @"86", @"164", @"242", 
						  @"8", @"86", @"164", @"242", @"8", @"86", @"164", @"164", nil]];
	[self setYCoordArray:[[NSArray alloc] initWithObjects:@"86", @"86", @"86", @"86", @"164", @"164", @"164", 
						  @"164", @"242", @"242", @"242", @"242", @"320", @"320", @"320", @"398", nil]];
	[self setButtonIdentity:[[NSArray alloc] initWithObjects:button1, button2, button3, 
							 button4, button5, button6, button7, button8, button9, 
							 buttonPosNeg, buttonClear, buttonPlus, buttonMinus, buttonDivide, 
							 buttonMultiply, buttonDecimal, nil]];
	[self setButtonIdentityRemain:[[NSMutableArray alloc] initWithArray:[self buttonIdentity]]];
	[self setXCoordRemain:[[NSMutableArray alloc] initWithArray:[self xCoordArray]]];
	[self setYCoordRemain:[[NSMutableArray alloc] initWithArray:[self yCoordArray]]];

	NSArray *messages = [[NSArray alloc] initWithObjects:@"You had to get a calculator out for that?",
	@"Here I am, brain the size of a planet, and they ask me to do this?!",
	@"Don't insult my intelligence!",
	@"Do it yourself.  Stupid ape.",
	@"Oy, fleshbag!  Try counting on your fingers!",
	@"Is *that* really worth bothering me?  I ask you.",
	@"Kids these days... don't they teach you to count in those schools of yours?", nil];

	self.snarkyMessages = messages;

	[messages release];
    [super viewDidLoad];
}

- (IBAction)PressedNum:(id)sender{
	if (currentButton != 0) {
		currentButton = (currentButton * 10.0f) + [[sender currentTitle] floatValue];
	}
	else {
		currentButton = [[sender currentTitle] floatValue];
	}
	[calcDisplay setText:[NSString stringWithFormat:@"%d", (int)currentButton]];
}
- (IBAction)PressedOperator:(id)sender {
	currentModifier = [modifier indexOfObject:[sender currentTitle]] + 1;
	previousButton = currentButton;	
	currentButton = 0.0f;
}
- (IBAction)PressedClear:(id)sender {
	currentButton = 0.0f;
	currentModifier = 0;
	previousButton = 0.0f;
	[calcDisplay setText:[NSString stringWithFormat:@"0"]];
}
- (IBAction)PressedPosNeg:(id)sender {
	currentButton = currentButton * -1.0f;
	[calcDisplay setText:[NSString stringWithFormat:@"%d", (int)currentButton]];
}
- (IBAction)PressedDecimal:(id)sender {}
- (IBAction)equalsPressed:(id)sender {
	switch (currentModifier)
	{
		case 1:
			[calcDisplay setText:[NSString stringWithFormat:@"%lf",
												   currentButton = previousButton + currentButton]];
			break;
			
		case 2:
			[calcDisplay setText:[NSString stringWithFormat:@"%lf",
												   currentButton = previousButton - currentButton]];
			break;
			
		case 3:
			if (currentButton == 0.0f ) // Checks for dividing by zero.
				[calcDisplay setText:@"Error!"];
			else	
				[calcDisplay setText:[NSString stringWithFormat:@"%lf",
													   currentButton = previousButton / currentButton]];
			break;
			
		case 4:
			[calcDisplay setText:[NSString stringWithFormat:@"%lf",
												   currentButton = previousButton * currentButton]];
			break;
			
			
		default:
			[calcDisplay setText:[NSString stringWithFormat:@"%d", (int)currentButton]];
	}
	
	previousModifier = currentModifier;
	currentModifier = 0;
}
- (IBAction)PressedRand:(id)sender {
	
	int i, a, b;
	UIButton *selectedButton = [[UIButton alloc] init]; //Generic placeholder for a button object.
	
	for (i = 0; i < 16; i++) {
		
		NSUInteger countArray = [buttonIdentityRemain count]; //count of remaining buttons to be randomised.
		a = random() %countArray;
		b = random() %countArray;
		//		while (a == b) b = random() %countArray;
		selectedButton = [self.buttonIdentityRemain objectAtIndex:b]; //Picks random object from list of button objects and sets it to the placeholder.
		
		//Sets the frame for the placeholder to a random x,y from the respective arrays.
		selectedButton.frame = CGRectMake([[[self xCoordRemain] objectAtIndex:a ] doubleValue], [[[self yCoordRemain] objectAtIndex:a ] doubleValue], 70, 70);
		
		//Removes the button, and the x,y coordinates from their arrays so can't be used again.
		[buttonIdentityRemain removeObjectAtIndex:b];
		[xCoordRemain removeObjectAtIndex:a];
		[yCoordRemain removeObjectAtIndex:a];
		//Changes the randomiser seed.
		srandom(time(NULL));
	}
	
	//Removes all objects from the arrays, then re-adds the objects from the immutable arrays, for the next button press.
	[buttonIdentityRemain removeAllObjects];
	[xCoordRemain removeAllObjects];
	[yCoordRemain removeAllObjects];
	
	[buttonIdentityRemain addObjectsFromArray:buttonIdentity];
	[xCoordRemain addObjectsFromArray:xCoordArray];
	[yCoordRemain addObjectsFromArray:yCoordArray];
	
	[selectedButton release];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	
	self.button0 = nil;
	self.button1 = nil;
	self.button2 = nil;
	self.button3 = nil;
	self.button4 = nil;
	self.button5 = nil;
	self.button6 = nil;
	self.button7 = nil;
	self.button8 = nil;
	self.button9 = nil;
	self.buttonPlus = nil;
	self.buttonDivide = nil;
	self.buttonMinus = nil;
	self.buttonMultiply = nil;
	self.buttonEquals = nil;
	self.buttonClear = nil;
	self.buttonPosNeg = nil;
	self.buttonRand = nil;
	 
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc {

	[button1 release];
	[button2 release];
	[button3 release];
	[button4 release];
	[button5 release];
	[button6 release];
	[button7 release];
	[button8 release];
	[button9 release];
	[button0 release];
	[buttonPlus release];
	[buttonMinus release];
	[buttonDivide release];
	[buttonMultiply release];
	[buttonClear release];
	[buttonEquals release];
	[buttonPosNeg release];	
	[buttonRand release];
    [super dealloc];
}

@end
